package com.cbgolf.oa.model;

import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.contract.IOrderDetailsContract.IOrderDetailsModel;
import com.cbgolf.oa.event.EventOrder;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.viewbean.OrderBean;
import org.greenrobot.eventbus.EventBus;

/**
 * @author lq
 * @date 2018/4/12
 */

public class OrderDetailsModelImp extends BaseModel implements IOrderDetailsModel {


  @Override
  public void requestData(String orderId, boolean isMatch) {
    Web.getOK((isMatch ? WebAPI.order_details_match_single_get :
        WebAPI.order_details_single_get) + orderId, new NetCallBack(response -> {
      EventBus.getDefault().post(new EventOrder.Builder()
          .setTarget(DataManager.ORDER_DETAILS)
          .setOrderBean((OrderBean) getBean(response, OrderBean.class))
          .setError(errorMsg, errorCode)
          .build());
    }));
  }

  @Override
  public void doSubmit(boolean isHotel, String orderId) {
    Web.putjsonOk((isHotel ? WebAPI.order_hotel_confirm_put :
        WebAPI.order_combo_confirm_put) + orderId, new NetCallBack(response -> {
      EventBus.getDefault().post(new EventOrder.Builder()
          .setTarget(DataManager.ORDER_DETAILS_CONFIRM)
          .setIsConfirmSuccess(NetUtils.SUCCESS == response.statusCode)
          .setIsHotel(isHotel)
          .setError(response.errorMsg, errorCode)
          .build());
      if (NetUtils.SUCCESS == response.statusCode) {
        requestData(orderId, false);
      }
    }));
  }

  @Override
  public void doCancel(boolean isHotel, String orderId) {
    Web.deleteOk((isHotel ? WebAPI.order_hotel_cancel_delete :
        WebAPI.order_combo_cancel_delete) + orderId, new NetCallBack(response -> {
      EventBus.getDefault().post(new EventOrder.Builder()
          .setTarget(DataManager.ORDER_DETAILS_CANCEL)
          .setIsCancelSuccess(NetUtils.SUCCESS == response.statusCode)
          .setIsHotel(isHotel)
          .setError(response.errorMsg, errorCode)
          .build());
      if (NetUtils.SUCCESS == response.statusCode) {
        requestData(orderId, false);
      }
    }));
  }


}
